package com.gugee.tiktok.web.controller;


import com.gugee.common.web.util.Ret;
import com.gugee.common.web.config.exception.ParamsException;
import com.gugee.common.web.system.promise.Limit;
import com.gugee.common.web.system.promise.ParamsType;
import com.gugee.common.web.system.promise.RequestLimit;
import com.gugee.common.web.system.user.log.Operation;
import com.gugee.tiktok.web.dto.ChallengeResponse;
import com.gugee.tiktok.web.dto.ChallengeSearchData;
import com.gugee.tiktok.web.service.ChallengeService;
import com.xiguaji.boot.toolkit.jdbc.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLDecoder;

@RestController
@Api(value = "标签",tags = {"标签"})
public class ChallengeController extends TikTokBaseController {

    @Autowired
    ChallengeService challengeService;

    @ApiOperation(value = "热门标签列表")
    @RequestMapping(value = "hashtag",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门标签列表")
    @RequestLimit(value = "MusicAndChallegeLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    public String getPage(String data) throws IOException, ParamsException {
        ChallengeSearchData searchData = getParam(ChallengeSearchData.class, URLDecoder.decode(data,"UTF-8"));
        Page<ChallengeResponse> pageData = challengeService.getPageEs(searchData,getUser().getId());
        return Ret.pageData(pageData.getTotalRow(),pageData.getList());
    }

    @ApiOperation(value = "热门标签详情信息")
    @RequestMapping(value = "hashtag/{cid}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门标签详情信息",fetchParam = "cid",paramType = ParamsType.URL)
    public String getDetail(@PathVariable long cid) throws IOException {
        return Ret.success("SUCCESS",challengeService.getChallenge(cid,getUser().getId()));
    }

    @ApiOperation(value = "热门标签国家详情趋势")
    @RequestMapping(value = "hashtag/region/distribution/{cid}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门标签国家详情趋势",fetchParam = "cid",paramType = ParamsType.URL)
    public String getRegionDistribution(@PathVariable long cid) throws IOException {
        return Ret.success("SUCCESS",challengeService.getRegionDistribution(cid));
    }
    @ApiOperation(value = "热门标签行业详情趋势")
    @RequestMapping(value = "hashtag/industry/distribution/{cid}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门标签行业详情趋势",fetchParam = "cid",paramType = ParamsType.URL)
    public String getIndustryDistribution(@PathVariable long cid) throws IOException {
        return Ret.success("SUCCESS",challengeService.getIndustryDistribution(cid));
    }

    @ApiOperation(value = "热门标签相关视频")
    @RequestMapping(value = "hashtag/videos/{cid}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门标签相关视频",fetchParam = "cid",paramType = ParamsType.URL)
    @RequestLimit(value = "MusicAndChallegeLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    public String getVideos(@PathVariable long cid,String data) throws IOException, ParamsException {
        int page = getParam(Integer.class, URLDecoder.decode(data,"UTF-8"), "page");
        int pageSize = getParam(Integer.class,URLDecoder.decode(data,"UTF-8"),"pageSize");
        int sortType = getParam(Integer.class,URLDecoder.decode(data,"UTF-8"),"sortType");
        val awemePage = challengeService.getAwemePageByChallenge(cid, sortType, page, pageSize, getUser().getId());
        return Ret.pageData(awemePage.getTotalRow(),awemePage.getList());
    }
}
